using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.Data;

namespace DBSourceToolsLib.SysObjects.UI
{
    public enum DBSourceListViewColType
    {
        Text,
        Checkbox
    }
    public class DBSourceListCol
    {
        public string ColName;
        public DBSourceListViewColType ColType;
        public int MinX;
        public int MaxX;
        public int DataSetCol;
    }
    

    public class DBSourceListView : ListView//, IComparer
    {
        private System.Data.DataSet m_DataSet;
        
        public System.Data.DataSet DataSet
        {
            get { return m_DataSet; }
            set { m_DataSet = value; }
        }

        public int CurrentColumn = -1;
        public int CurrentRow = -1;

        List<DBSourceListCol> m_ColList = new List<DBSourceListCol>();
        Point m_LastMouseLocation;

        Hashtable m_HashColumns = new Hashtable();
        private bool[] arrCheckColumns;
        private int arrCheckSize = 0;
        private int xmin = 0;
        private int xmax = 0;
        public bool m_isCheckbox = false;
        Bitmap m_CheckBoxOn = (Bitmap)DBSourceToolsLib.Properties.Resources.ImageCheckBoxOn;
        Bitmap m_CheckBoxOff = (Bitmap)DBSourceToolsLib.Properties.Resources.ImageCheckBoxOff;

        public DBSourceListView()
        {
            this.View = View.Details;
            this.FullRowSelect = true;
            //this.ListViewItemSorter = this;
            this.ColumnClick += new ColumnClickEventHandler(DBListView_ColumnClick);
            this.DrawSubItem += new DrawListViewSubItemEventHandler(DBSourceListView_DrawSubItem);
            this.DrawItem += new DrawListViewItemEventHandler(DBSourceListView_DrawItem);
            this.DrawColumnHeader += new DrawListViewColumnHeaderEventHandler(DBSourceListView_DrawColumnHeader);
            this.ItemSelectionChanged += new ListViewItemSelectionChangedEventHandler(DBSourceListView_ItemSelectionChanged);
            this.MouseMove += new MouseEventHandler(DBSourceListView_MouseMove);
            this.DoubleBuffered = true;
        }

        void DBSourceListView_MouseMove(object sender, MouseEventArgs e)
        {
            m_LastMouseLocation = e.Location;
            // find column number.
            //CurrentColumn = FindCurrentColumn(e.X, e.Y);
        }

        private int FindCurrentColumn(int _x, int _y)
        {
            for (int i = 0; i < this.m_ColList.Count; i++)
            {
                DBSourceListCol oCol = this.m_ColList[i];
                if ((_x >= oCol.MinX) && (_x <= oCol.MaxX))
                    return i;

            }
            return -1;
        }

        void DBSourceListView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawBackground();
            e.DrawText();
        }
        public void AddColumn(string colName, DBSourceListViewColType oType)
        {
            m_HashColumns.Add(colName, oType);
            DBSourceListCol oCol = new DBSourceListCol();
            oCol.ColName = colName;
            oCol.ColType = oType;
            this.m_ColList.Add(oCol);
            // check that the col idx is correct
            for( int i = 0; i < this.DataSet.Tables[0].Columns.Count; i++) {
                if (this.DataSet.Tables[0].Columns[i].ColumnName.CompareTo(colName) == 0)
                {
                    oCol.DataSetCol = i;
                    break;
                }
            }
            
        }

        public void Bind()
        {
            this.arrCheckSize = this.m_ColList.Count;
            this.arrCheckColumns = new bool[arrCheckSize];
            for (int i = 0; i < this.arrCheckSize; i++)
            {
                DBSourceListCol oCol = this.m_ColList[i];
                if (oCol.ColType == DBSourceListViewColType.Checkbox)
                    arrCheckColumns[i] = true;
                else
                    arrCheckColumns[i] = false;
            }

            this.SuspendLayout();
            addRows();
            this.ResumeLayout();
        }

        private void addRows()
        {
            this.Items.Clear();
            if (this.DataSet.Tables.Count > 0)
            {
                DataTable oDataTable = this.DataSet.Tables[0];
                foreach (DataRow oDataRow in oDataTable.Rows)
                {
                    List<string> subItemArray = new List<string>();
                    foreach (DBSourceListCol oCol in this.m_ColList)
                    {
                        subItemArray.Add(oDataRow[oCol.ColName].ToString());

                    }
                    ListViewItem li = this.Items.Add(new ListViewItem(subItemArray.ToArray()));
                }
            }
        }

        //public int Compare(object x, object y)
        //{
        //    //string strX = ((ListViewItem)x).SubItems[CurrentColumn].Text;
        //    //string strY = ((ListViewItem)y).SubItems[CurrentColumn].Text;

        //    //if (DataSource.GetItemProperties(null)[CurrentColumn].PropertyType == typeof(DateTime))
        //    //{
        //    //    return ((this.Sorting == SortOrder.Descending) ? -1 : 1) *
        //    //        DateTime.Compare(DateTime.Parse(strX), DateTime.Parse(strY));
        //    //}
        //    //else
        //    //{
        //    //    return ((this.Sorting == SortOrder.Descending) ? -1 : 1) *
        //    //        string.Compare(strX, strY);
        //    //}
        //}

        void DBListView_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            //e.Column = columnindex;
            if (this.arrCheckColumns[e.Column])
            {
                int dataSourceColIdx = this.m_ColList[e.Column].DataSetCol;
                foreach (DataRow oRow in this.DataSet.Tables[0].Rows)
                {
                    oRow[dataSourceColIdx] = !(bool)oRow[dataSourceColIdx];
                }
            }
            this.Invalidate();
        }

        void DBSourceListView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            int col_min_x = -1, col_max_x = -1;

            e.DrawBackground();

            if (e.Item.Selected)
            {
                // Draw the background and focus rectangle for a selected item.
                e.Graphics.FillRectangle(Brushes.LightGray, e.Bounds);
                //e.DrawFocusRectangle(e.Bounds);
            }

            if (!this.arrCheckColumns[e.ColumnIndex])
            {
                e.DrawText();
                col_min_x = e.Bounds.X;
                col_max_x = e.Bounds.X + e.Bounds.Width;
            }
            else
            {
                Bitmap oCheckBox;
                int dataSourceColIdx = this.m_ColList[e.ColumnIndex].DataSetCol;
                //string strItemString = this.DataSet.Tables[0].Rows[e.ItemIndex][dataSourceColIdx].ToString();
                if ((bool)this.DataSet.Tables[0].Rows[e.ItemIndex][dataSourceColIdx])
                    oCheckBox = m_CheckBoxOn;
                else
                    oCheckBox = m_CheckBoxOff;

                int x = (e.Bounds.X + e.Bounds.Width / 2) - (oCheckBox.Width / 2);
                col_min_x = x;
                col_max_x = x + oCheckBox.Width;
                int y = (e.Bounds.Y + 1);
                e.Graphics.DrawImage(oCheckBox, x, y);
                //e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
                //e.DrawFocusRectangle(e.Bounds);
            }
            this.m_ColList[e.ColumnIndex].MinX = col_min_x;
            this.m_ColList[e.ColumnIndex].MaxX = col_max_x;

        }

        void DBSourceListView_DrawItem(object sender, DrawListViewItemEventArgs e)
        {
            
            //DBSourceToolsLib.Properties.Resources.ImageCheckBoxOn
            if ((e.State & ListViewItemStates.Selected) != 0)
            {
                // Draw the background and focus rectangle for a selected item.
                
                e.Graphics.FillRectangle(Brushes.Maroon, e.Bounds);
                e.DrawFocusRectangle();
            }
            else
            {
                e.DrawFocusRectangle();
            }
            if (this.View != View.Details)
            {
                e.DrawText();
            }

        }

        void DBSourceListView_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
        {
            
           CurrentColumn = FindCurrentColumn(m_LastMouseLocation.X, m_LastMouseLocation.Y);
           if (e.IsSelected)
            {
                this.CurrentRow = e.ItemIndex;
               if (this.CurrentColumn != -1)
                {
                    if (this.arrCheckColumns[this.CurrentColumn])
                    {
                        int dataSourceColIdx = this.m_ColList[this.CurrentColumn].DataSetCol;
                        this.DataSet.Tables[0].Rows[e.ItemIndex][dataSourceColIdx]
                            = !(bool)this.DataSet.Tables[0].Rows[e.ItemIndex][dataSourceColIdx];
                    }
                }
            }

        }

    }
}
